package jsyf.oa.dao.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.system.RoleEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: 徐波
 * @creat: 2017/11/23 0023 9:59
 * description:
 */
@Repository
public class RoleDao extends BaseDao {
    public RoleDao() {
        super.iBaseEntity = new RoleEntity();
    }

    public RoleEntity getEntityById(String id){
        return (RoleEntity) super.getIBaseEntityById(id);
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, RoleEntity.class);
    }

    public Boolean frontFlagIs(String roleId){
        JSONObject obj = getViewJSONById(roleId);
        return obj.getInteger("frontFlag") == 1;
    }

    public Boolean sort(String roleIds){
        List<String> list = BaseJdbcDao.stringToList(roleIds);
        for (int i = 0; i < list.size(); i++) {
            JSONObject obj = new JSONObject();
            obj.put("ID",list.get(i));
            obj.put("SORT_NO",i);

            JSONObject objId = new JSONObject();
            objId.put("ID",list.get(i));
            super.update(iBaseEntity.getTableName(),obj,objId);
        }
        return true;
    }


}
